<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="addfn"
            >新增角色</el-button>
            <!-- 表格 -->
            <el-table v-loading="loading" :data="list">
              <el-table-column label="序号" width="100" type="index" :index="totaladdfn" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="RoleSetting(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="update(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delfn(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              :current-page="page"
              :page-sizes="[1, 2, 3, 5, 10 , 20 , 30 , 50]"
              :page-size="pagesize"
              layout="total , sizes,  prev, pager, next"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <!-- 公司信息 -->
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyForm.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyForm.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyForm.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyForm.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <Setting ref="setted" :show-dialog="showDialog" @closefn="closeed" />
    <!-- 分配权限的弹层 -->
    <el-dialog title="分配权限" :visible="showAssignDialog" @close="closeAssignDialog" @open="getSetIng">
      <el-tree
        ref="tree"
        v-loading="loading"
        :data="getSetIngList"
        :props="{label: 'name'}"
        show-checkbox
        :check-strictly="true"
        default-expand-all
        node-key="id"
      />
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="closeAssignDialog">取消</el-button>
          <el-button type="primary" @click="SetingAddfn">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import Setting from './compents/index.vue'
import { getUSerInfoAPI, deleteUSerInfoAPI, getpeoplefoAPI, SetingListAPI } from '@/api/setting'
import { getCoreAPI } from '@/api/company'
import { getUserInfoAPP } from '@/api/permission'
import { transList } from '@/utils/index'
export default {
  name: 'Ssetting',
  components: {
    Setting
  },
  data() {
    return {
      page: 1,
      pagesize: 5,
      list: [],
      total: null,
      loading: false,
      showDialog: false,
      companyForm: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      },
      showAssignDialog: false,
      roleId: '',
      getSetIngList: [],
      permIds: []
    }
  },
  computed: {
    totaladdfn() {
      return (this.page - 1) * this.pagesize + 1
    }
  },
  created() {
    this.getuserINfo()
    this.getCore()
  },
  methods: {
    // 分配角色
    async  SetingAddfn() {
      const permIdsList = this.$refs.tree.getCheckedKeys()
      // console.log(permIdsList)
      await SetingListAPI({
        id: this.roleId,
        permIds: permIdsList
      })
      this.$message.success('分配权限成功')
      this.closeAssignDialog()
    },
    /**
     * 分配权限功能
     */
    // 关闭弹框
    closeAssignDialog() {
      this.showAssignDialog = false
    },
    // ! 点击分配权限展示弹层 拿到id
    async  RoleSetting(id) {
      this.showAssignDialog = true
      this.roleId = id
      this.loading = true
      const { data } = await getUserInfoAPP()
      // console.log(data)
      this.getSetIngList = transList(data, '0')
      const { data: { permIds }} = await getpeoplefoAPI(id)
      this.permIds = permIds
      this.$refs.tree.setCheckedKeys(this.permIds)
      this.loading = false
    },
    // todo 拿到接口树形渲染
    async getSetIng() {

      // console.log(this.getSetIngList)
    },
    /**
     * 获取公司设置
     */
    async getCore() {
      const id = this.$store.state.user.userInfo.companyId
      const res = await getCoreAPI(id)
      console.log(res)
      this.companyForm = res.data
    },
    async getuserINfo() {
      this.loading = true
      const res = await getUSerInfoAPI({
        page: this.page,
        pagesize: this.pagesize
      })
      console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
      this.loading = false
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.getuserINfo()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.getuserINfo()
    },
    delfn(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteUSerInfoAPI(id)
        console.log(res)
        this.$message.success('删除成功')
        if (this.list.length === 1 && this.page > 1) {
          this.page--
        }
        this.getuserINfo()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    addfn() {
      this.showDialog = true
    },
    closeed() {
      this.showDialog = false
    },
    async update(row) {
      console.log(row)
      this.showDialog = true
      const res = await getpeoplefoAPI(row.id)
      console.log(res)
      this.peopleList = res.data
      this.$refs.setted.form = res.data
    }
  }
}
</script>

<style scoped>
  .el-pagination{
    text-align: center;
    margin-top: 20px;
  }
</style>
